/*
 * #%L
 * HAPI FHIR JPA Server
 * %%
 * Copyright (C) 2014 - 2025 Smile CDR, Inc.
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package ca.uhn.fhir.jpa.entity;

import ca.uhn.fhir.jpa.model.entity.BasePartitionable;
import ca.uhn.fhir.jpa.model.entity.IdAndPartitionId;
import ca.uhn.fhir.util.ValidateUtil;
import jakarta.annotation.Nonnull;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.Index;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinColumns;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.PrePersist;
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.Table;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hibernate.Length;

import java.io.Serializable;
import java.util.Objects;

import static org.apache.commons.lang3.StringUtils.left;
import static org.apache.commons.lang3.StringUtils.length;

@Entity
@Table(
		name = "TRM_CONCEPT_DESIG",
		uniqueConstraints = {},
		indexes = {
			// must have same name that indexed FK or SchemaMigrationTest complains because H2 sets this index
			// automatically
			@Index(name = "FK_CONCEPTDESIG_CONCEPT", columnList = "CONCEPT_PID", unique = false),
			@Index(name = "FK_CONCEPTDESIG_CSV", columnList = "CS_VER_PID")
		})
@IdClass(IdAndPartitionId.class)
public class TermConceptDesignation extends BasePartitionable implements Serializable {
	private static final long serialVersionUID = 1L;

	public static final int MAX_LENGTH = 500;
	public static final int MAX_VAL_LENGTH = 2000;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns(
			value = {
				@JoinColumn(
						name = "CONCEPT_PID",
						referencedColumnName = "PID",
						insertable = false,
						updatable = false,
						nullable = false),
				@JoinColumn(
						name = "PARTITION_ID",
						referencedColumnName = "PARTITION_ID",
						insertable = false,
						updatable = false,
						nullable = false)
			},
			foreignKey = @ForeignKey(name = "FK_CONCEPTDESIG_CONCEPT"))
	private TermConcept myConcept;

	@Column(name = "CONCEPT_PID", insertable = true, updatable = true, nullable = false)
	private Long myConceptPid;

	@Id()
	@SequenceGenerator(name = "SEQ_CONCEPT_DESIG_PID", sequenceName = "SEQ_CONCEPT_DESIG_PID")
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_CONCEPT_DESIG_PID")
	@Column(name = "PID")
	private Long myId;

	@Column(name = "LANG", nullable = true, length = MAX_LENGTH)
	private String myLanguage;

	@Column(name = "USE_SYSTEM", nullable = true, length = MAX_LENGTH)
	private String myUseSystem;

	@Column(name = "USE_CODE", nullable = true, length = MAX_LENGTH)
	private String myUseCode;

	@Column(name = "USE_DISPLAY", nullable = true, length = MAX_LENGTH)
	private String myUseDisplay;

	@Column(name = "VAL", nullable = true, length = MAX_VAL_LENGTH)
	private String myValue;

	@Column(name = "VAL_VC", nullable = true, length = Length.LONG32)
	private String myValueVc;
	/**
	 * TODO: Make this non-null
	 *
	 * @since 3.5.0
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns(
			value = {
				@JoinColumn(
						name = "CS_VER_PID",
						insertable = true,
						updatable = false,
						nullable = false,
						referencedColumnName = "PID"),
				@JoinColumn(
						name = "PARTITION_ID",
						referencedColumnName = "PARTITION_ID",
						insertable = true,
						updatable = false,
						nullable = false)
			},
			foreignKey = @ForeignKey(name = "FK_CONCEPTDESIG_CSV"))
	private TermCodeSystemVersion myCodeSystemVersion;

	public String getLanguage() {
		return myLanguage;
	}

	public TermConceptDesignation setLanguage(String theLanguage) {
		ValidateUtil.isNotTooLongOrThrowIllegalArgument(
				theLanguage,
				MAX_LENGTH,
				"Language exceeds maximum length (" + MAX_LENGTH + "): " + length(theLanguage));
		myLanguage = theLanguage;
		return this;
	}

	public String getUseCode() {
		return myUseCode;
	}

	public TermConceptDesignation setUseCode(String theUseCode) {
		ValidateUtil.isNotTooLongOrThrowIllegalArgument(
				theUseCode, MAX_LENGTH, "Use code exceeds maximum length (" + MAX_LENGTH + "): " + length(theUseCode));
		myUseCode = theUseCode;
		return this;
	}

	public String getUseDisplay() {
		return myUseDisplay;
	}

	public TermConceptDesignation setUseDisplay(String theUseDisplay) {
		myUseDisplay = left(theUseDisplay, MAX_LENGTH);
		return this;
	}

	public String getUseSystem() {
		return myUseSystem;
	}

	public TermConceptDesignation setUseSystem(String theUseSystem) {
		ValidateUtil.isNotTooLongOrThrowIllegalArgument(
				theUseSystem,
				MAX_LENGTH,
				"Use system exceeds maximum length (" + MAX_LENGTH + "): " + length(theUseSystem));
		myUseSystem = theUseSystem;
		return this;
	}

	public String getValue() {
		return Objects.nonNull(myValueVc) ? myValueVc : myValue;
	}

	public TermConceptDesignation setValue(@Nonnull String theValueVc) {
		myValueVc = theValueVc;
		return this;
	}

	public TermConceptDesignation setCodeSystemVersion(TermCodeSystemVersion theCodeSystemVersion) {
		myCodeSystemVersion = theCodeSystemVersion;
		return this;
	}

	public TermConceptDesignation setConcept(TermConcept theConcept) {
		myConcept = theConcept;
		myConceptPid = theConcept.getId();
		setPartitionId(theConcept.getPartitionId());
		return this;
	}

	@PrePersist
	public void prePersist() {
		if (myConceptPid == null) {
			myConceptPid = myConcept.getId();
			assert myConceptPid != null;
		}
	}

	public Long getPid() {
		return myId;
	}

	public IdAndPartitionId getPartitionedId() {
		return IdAndPartitionId.forId(myId, this);
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
				.append("conceptPid", myConcept.getId())
				.append("pid", myId)
				.append("language", myLanguage)
				.append("useSystem", myUseSystem)
				.append("useCode", myUseCode)
				.append("useDisplay", myUseDisplay)
				.append("value", myValue)
				.toString();
	}

	public Long getId() {
		return myId;
	}
}
